'''An anti-highlight and repeated highlight module.'''
############################################################################
#    Copyright (C) 2006 by Robert Small                                    #
#    SmallR2002@Gmail.com                                                  #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################

__module_name__        = "bchighlight"
__module_version__     = "0.0.2"
__module_description__ = "Anti-Highlight module."

#TODO: Detect mass user highlights, and trigger ban from that.
#FIXME: Make it default a count() if it has no set variable for the count.Moved to bcsettings.py

# 
# 0.0.2  Added some extra randomisation to it (the whole naming deal).
# 
# 0.0.1  Basics.
# 

import xchat
import sys
import time
import os
import random

if (sys.path.count(os.path.dirname(__file__)) == 0):
    sys.path.append(os.path.dirname(__file__))

#  Pretty much essential.
import bcsettings
import bcmisc

def handle_highlights(word, word_eol, userdata): #IGNORE:W0613
    '''
    Handle a highlight.
    @param word: all the data you need to work with, chopped up into small parts.
    @type word: string
    @param word_eol: all the data you need to work with, chopped from wherever you're starting from.
    @type word_eol: string
    @param userdata: custom object to pass back.
    @type userdata: object
    '''
    #Is it someone being an annoying little dweeb? Or just highlighting us (and possibly being an annoying little dweeb)?
    if (word[1].strip(" ").count(" ") == 0):
        #Yey us, it's a dweeb.
        if (word[0] == bcmisc.highlight_last_person) and (xchat.get_info('channel') == bcmisc.highlight_last_room) and (time.time() < (bcmisc.highlight_last_time + bcsettings.highlight_last_timeout)):
            #They overstepped the boundry! Now to find out a response.
            #Firstly, tell them off.
            itemp = int(random.random()*10) 
            
            if (itemp < 3):
                xchat.command('msg ' + xchat.get_info('channel') + ' ' + word[0] + ': ' + bcsettings.highlight_response[bcmisc.highlight_response_count])
            elif (itemp < 6) and (itemp >= 3):
                xchat.command('msg ' + xchat.get_info('channel') + ' ' + word[0] + ', ' + bcsettings.highlight_response[bcmisc.highlight_response_count])
            else:
                xchat.command('msg ' + xchat.get_info('channel') + ' ' + bcsettings.highlight_response[bcmisc.highlight_response_count])
            
            
            for i, w in enumerate(bcsettings.highlight_channels_ban):
                if (xchat.get_info('channel').lower() == bcsettings.highlight_channels_ban[i].lower()) and (xchat.get_info('network').lower() == bcsettings.highlight_networks_ban[i].lower()):
                    users = xchat.get_list('users')
                    user = find_user(users, word[0])
                    ban = '*!' + users[user].host
                    xchat.command('down ' + word[0])
                    xchat.command('ban ' + ban)
                    xchat.command('timer 60 unban ' + ban)
                fake = w
                w = fake
            #Next
        
        
            bcmisc.highlight_response_count = bcmisc.highlight_response_count + 1
        if (bcmisc.highlight_response_count > bcsettings.highlight_responses): 
            bcmisc.highlight_response_count = 0
        
        bcmisc.highlight_last_person = word[0]
        bcmisc.highlight_last_room = xchat.get_info('channel')
        bcmisc.highlight_last_time = time.time()

def find_user(users, user):
    '''
    Find which user is the one we want.
    @param users: user list.
    @type users: 'list'
    @param user: the user to find.
    @type user: string
    '''
    i = -1
    
    for u in users:
        i = i + 1
        if (u.nick == user):
            return i
    
    xchat.prnt('Help! no such user!')
    return -1